/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.dhecp.core.layui;

import com.dhecp.project.base.domain.BaseInfo;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
 * @author DH-Xxj
 * @version 2019年9月9日
 */
@Component
public class TreeDataUtil {
	
	/**
	 * 递归处理树的各级子节点  children
	 * @param treeDataModelList
	 * @return treeDataModelListReturn
	 * @author DH-Xxj
	 */
	
	public List<TreeDataModel> dealTreeDataModel(List<TreeDataModel> treeDataModelList, int maxlevels){
		int thislevels = maxlevels - 1;
		if (thislevels < 1){
			thislevels = 10;
		}
		List<TreeDataModel> treeDataModelListReturn = new ArrayList<TreeDataModel>();
		if (thislevels > 0){
			for (TreeDataModel treeDataModel:treeDataModelList){
				if (thislevels == treeDataModel.getLevels()){
					List<TreeDataModel> treeDataModelListChildren = new ArrayList<TreeDataModel>();
					for (TreeDataModel treeDataModelchildren:treeDataModelList){
						if (treeDataModel.getId().equals(treeDataModelchildren.getParentId())){
							treeDataModelListChildren.add(treeDataModelchildren);
						}
					}
					treeDataModel.setChildren(treeDataModelListChildren);
				}
			}
			if (thislevels > 1){//递归
				this.dealTreeDataModel(treeDataModelList, thislevels);
			}
		}

		for (TreeDataModel treeDataModel:treeDataModelList){
			if (1 == treeDataModel.getLevels()){
				treeDataModelListReturn.add(treeDataModel);
			}
			
		}
		return treeDataModelListReturn;
		
	}


	/**
	 * 递归处理树的各级子节点  children
	 * @param treeDataModelList
	 * @return treeDataModelListReturn
	 * @author DH-Xxj
	 */

	public List<TreeDataModel> dealTreeDataModelPc(List<TreeDataModel> treeDataModelList, int maxlevels,List<BaseInfo> baseInfoList){
		int thislevels = maxlevels - 1;
		if (thislevels < 1){
			thislevels = 10;
		}
		List<TreeDataModel> treeDataModelListReturn = new ArrayList<TreeDataModel>();
		if (thislevels > 0){
			for (TreeDataModel treeDataModel:treeDataModelList){
				if (thislevels == treeDataModel.getLevels()){ //二级菜单
//					Map<String, Object> exceptions = new HashMap<String, Object>();
//					exceptions.put(baseInfo.getVid(),treeDataModelListChildren);

					List<TreeDataModel> treeDataModelListChildren2 = new ArrayList<TreeDataModel>();

					for (BaseInfo baseInfo:baseInfoList
					) {
						List<TreeDataModel> treeDataModelListChildren = new ArrayList<TreeDataModel>();
						for (TreeDataModel treeDataModelchildren:treeDataModelList){
							if (baseInfo.getVid().equals(treeDataModelchildren.getModuleGroup())){//相同分类
								if (treeDataModel.getId().equals(treeDataModelchildren.getParentId())){ //二级所属的三级菜单
									treeDataModelListChildren.add(treeDataModelchildren);
								}
							}
						}
						TreeDataModel treeDataModel1 = new TreeDataModel();
						treeDataModel1.setTitle(baseInfo.getVid());
						treeDataModel1.setChildren(treeDataModelListChildren);
						treeDataModelListChildren2.add(treeDataModel1);
					}
					treeDataModel.setChildren(treeDataModelListChildren2);
				}
			}
			if (thislevels > 1){//递归
				this.dealTreeDataModel(treeDataModelList, thislevels);
			}
		}

//		List<TreeDataModel> treeDataModelListReturn = new ArrayList<TreeDataModel>();
//		for (TreeDataModel treeDataModel:treeDataModelList){
//			if (1 == treeDataModel.getLevels()){
//				treeDataModelListReturn.add(treeDataModel);
//			}
//
//		}
//
		for (TreeDataModel treeDataModel:treeDataModelList){
			if (1 == treeDataModel.getLevels()){
				treeDataModelListReturn.add(treeDataModel);
			}

		}
		return treeDataModelListReturn;

	}

}
